//
//  HWRegistTypePieCell.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/2/7.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import Charts
class HWRegistTypePieCell: UICollectionViewCell {
    var bgView:UIView!
    //饼状图
    var chartView: HWPieChartView!
    
    var model:HWRegistTypeModel? {
        didSet {
            //生成5条随机数据
            let labels = model?.reg_type
            let dataEntries = (0..<(model?.list.count ?? 0)).map { (i) -> PieChartDataEntry in
                return PieChartDataEntry(value: model?.list[i].count ?? 0,
                                         label: labels?[i])
            }
            let chartDataSet = PieChartDataSet(entries: dataEntries, label: "")
            chartDataSet.drawValuesEnabled = false
            //        chartDataSet.selectionShift = 300
            //设置颜色
            //        chartDataSet.colors = ChartColorTemplates.vordiplom()
            //        + ChartColorTemplates.joyful()
            //        + ChartColorTemplates.colorful()
            //        + ChartColorTemplates.liberty()
            //        + ChartColorTemplates.pastel()
            
            chartDataSet.colors = [.colorWithHexString(hexString: "#EA7640"),
                                   .colorWithHexString(hexString: "#FBCF57"),
                                   .colorWithHexString(hexString: "#F2E440"),
                                   .colorWithHexString(hexString: "#FAFF2F"),
                                   .colorWithHexString(hexString: "#BBE717"),
                                   .colorWithHexString(hexString: "#65D863"),
                                   .colorWithHexString(hexString: "#43C7E9"),
                                   .colorWithHexString(hexString: "#339AE5"),
                                   .colorWithHexString(hexString: "#699AFF"),
                                   .colorWithHexString(hexString: "#B862E6"),
                                   .colorWithHexString(hexString: "#C14FA4"),
                                   .colorWithHexString(hexString: "#EA6453"),
                                   
            ]
            chartDataSet.entryLabelColor = .colorWithAlphaHexString(hexString: "#000000", alpha: 0.65)
            chartDataSet.xValuePosition = .outsideSlice //标签显示在内
            //        chartDataSet.yValuePosition = .outsideSlice //数值显示在外
            chartDataSet.valueLineVariableLength = true
            
            chartDataSet.valueLinePart1OffsetPercentage = 1 //折线中第一段起始位置相对于扇区偏移量（值越大距越远）
            chartDataSet.valueLinePart1Length = 0.5 //折线中第一段长度占比
            chartDataSet.valueLinePart2Length = 0.5 //折线中第二段长度最大占比
            chartDataSet.valueLineWidth = 1 //折线的粗细
            chartDataSet.valueLineColor = UIColor.colorWithAlphaHexString(hexString: "#000000", alpha: 0.45) //折线颜色
            let chartData = PieChartData(dataSet: chartDataSet)
            chartData.setValueTextColor(.black)//文字颜色为黑色
            
            //设置饼状图数据
            chartView.data = chartData
            chartView.rotationAngle = 180
        }
    }
    
    var rechargeTypeModel:HWRechargeTypeModel? {
        didSet {
            //生成5条随机数据
            let labels = rechargeTypeModel?.payment
            let dataEntries = (0..<(rechargeTypeModel?.list.count ?? 0)).map { (i) -> PieChartDataEntry in
                return PieChartDataEntry(value: rechargeTypeModel?.list[i].count ?? 0,
                                         label: labels?[i])
            }
            let chartDataSet = PieChartDataSet(entries: dataEntries, label: "")
            chartDataSet.drawValuesEnabled = false
            //        chartDataSet.selectionShift = 300
            //设置颜色
            //        chartDataSet.colors = ChartColorTemplates.vordiplom()
            //        + ChartColorTemplates.joyful()
            //        + ChartColorTemplates.colorful()
            //        + ChartColorTemplates.liberty()
            //        + ChartColorTemplates.pastel()
            
            //            chartDataSet.colors = [.colorWithAlphaHexString(hexString: "#4475FB", alpha: 0.85),.colorWithAlphaHexString(hexString: "#50D9A3", alpha: 0.85),.colorWithAlphaHexString(hexString: "#637CA9", alpha: 0.85),.colorWithAlphaHexString(hexString: "#FFCA29", alpha: 0.85),.colorWithAlphaHexString(hexString: "#F56644", alpha: 0.85),.red,.yellow,.blue,.brown,.purple,.cyan]
            chartDataSet.colors = [.colorWithHexString(hexString: "#EA7640"),
                                   .colorWithHexString(hexString: "#FBCF57"),
                                   .colorWithHexString(hexString: "#F2E440"),
                                   .colorWithHexString(hexString: "#FAFF2F"),
                                   .colorWithHexString(hexString: "#BBE717"),
                                   .colorWithHexString(hexString: "#65D863"),
                                   .colorWithHexString(hexString: "#43C7E9"),
                                   .colorWithHexString(hexString: "#339AE5"),
                                   .colorWithHexString(hexString: "#699AFF"),
                                   .colorWithHexString(hexString: "#B862E6"),
                                   .colorWithHexString(hexString: "#C14FA4"),
                                   .colorWithHexString(hexString: "#EA6453"),
                                   
            ]
            chartDataSet.entryLabelColor = .colorWithAlphaHexString(hexString: "#000000", alpha: 0.65)
            chartDataSet.xValuePosition = .outsideSlice //标签显示在内
            //        chartDataSet.yValuePosition = .outsideSlice //数值显示在外
            chartDataSet.valueLineVariableLength = true
            
            chartDataSet.valueLinePart1OffsetPercentage = 1 //折线中第一段起始位置相对于扇区偏移量（值越大距越远）
            chartDataSet.valueLinePart1Length = 0.5 //折线中第一段长度占比
            chartDataSet.valueLinePart2Length = 0.5 //折线中第二段长度最大占比
            chartDataSet.valueLineWidth = 1 //折线的粗细
            chartDataSet.valueLineColor = UIColor.colorWithAlphaHexString(hexString: "#000000", alpha: 0.45) //折线颜色
            let chartData = PieChartData(dataSet: chartDataSet)
            chartData.setValueTextColor(.black)//文字颜色为黑色
            
            //设置饼状图数据
            chartView.data = chartData
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWRegistTypePieCell {
    private func initUI() -> Void {
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 12*widthScale
        contentView.addSubview(bgView)
        
        //创建饼图组件对象
        chartView = HWPieChartView()
        //        chartView.frame = CGRect(x: 20*widthScale, y: 25*widthScale, width: screenWidth-90*widthScale,height: 303*widthScale)
        //        chartView.holeRadiusPercent = 0
        chartView.transparentCircleColor = .white
        //        chartView.drawHoleEnabled = false
        //标记，指示是否应该绘制入口标签。默认 YES
        //        chartView.drawEntryLabelsEnabled = false
        chartView.legend.enabled = true
        chartView.legend.font = .systemFont(ofSize: 12*widthScale)
        chartView.legend.form = .circle
        chartView.legend.xEntrySpace = 15*widthScale
        chartView.legend.yEntrySpace = 6*widthScale
        chartView.legend.formSize = 9*widthScale
        chartView.rotationEnabled = true
//        chartView.isUserInteractionEnabled = false
        chartView.minOffset = 26*widthScale
        bgView.addSubview(chartView)
        
    }
    
    private func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(15*widthScale)
        }
        
        chartView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalTo(-20*widthScale)
        }
    }
}
